//************************************************
// File Name: ListViewController.m
// Team: Team Epsilon
// Date Created: 2009/07/12
// Purpose: Show the top score completed by player. 
// Change History: 
//   * James, Date: 2009/07/12 Create File
//   * James, Date: 2009/07/14 Create variable to save scores.
//   * James, Date: 2009/07/15 Create function to display the socre. 
//   * Mark, Date: 2009/07/15 Create adding function to sum all levels.
//   * JJ, Date: 2009/07/15 Fix some score update bugs.
//   * Mark, Date: 2009/07/16 Fix sometimes that the score not display correctly.
//   * JJ, Date: 2009/07/16 Display improvments.
//   * James, Date: 2009/07/16 Display improvements.
//   * James, Date: 2009/07/16 Adding Comments.
// Known Bugs:
//   1. Only Show the top score the player has complete.
//   2. The top score achieve need to be reset manually using the code.
//************************************************

#import "ListViewController.h"
#import "Level0ViewController.h"

//implementation of list view controller
@implementation ListViewController

//to display the name
@synthesize name;

//variable to get the level score 
@synthesize level0,level1;

/*
-(IBAction) updatePrefs:(id) sender {
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
	[prefs setObject:name.text forKey:@"score0"];
	[name resignFirstResponder];
	
	score0.text = @"Application Preference Saved!";
}
*/

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	//update everytime the score board selected
	timer = [NSTimer scheduledTimerWithTimeInterval:(1.1) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

//display the score everytime it loads
//clear score can by done here
-(void)onTimer
{
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
	
	/*this line reset the highscore to zero*/
	//[prefs setInteger:0 forKey:@"top"];
	int top_score = [prefs integerForKey:@"top"]; //top score save here

	int level0_score = [prefs integerForKey:@"level0"];  //tutorial level score save here
	
	int level1_score= [prefs integerForKey:@"level1"]; //level 1 score save here

	int total_score = level1_score + level0_score; //calculate the total score
	
	//display the score
	if (top_score == nil)
	{
		[prefs setInteger:total_score forKey:@"top"];
		
	}
	else
	{
		if (total_score > top_score)
		{
			[prefs setInteger:total_score forKey:@"top"];
		}
	}
	
	top_score = [prefs integerForKey:@"top"];
	
	level1.text = [NSString stringWithFormat:@"%04d ", top_score];

}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

// memory warning 
- (void)didReceiveMemoryWarning 
{
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

//unload the interface
- (void)viewDidUnload 
{
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

//deallocate the memory
- (void)dealloc 
{
	[timer release];
    [super dealloc];
}


@end
